package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.view.View;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.MultiItemTypeAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.RepositoryEntity;

/**
 * Created by qiuzhenhuan on 2019/7/30.
 */

public class ADA_SelectRepository extends CommonAdapter<RepositoryEntity> {

    private int selectedPository = 0;
    private ViewHolder mPreHolder;

    public ADA_SelectRepository(Context context) {
        super(context);
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_select_repository;
    }

    @Override
    protected void convert(final ViewHolder holder, RepositoryEntity repositoryEntity, final int position) {

        if (selectedPository == position) {
            mPreHolder = holder;
        }
        holder.setChecked(R.id.rb_check, selectedPository == position);
        holder.setText(R.id.tv_repository, StringUtils.get(repositoryEntity.getRepositoryName()));
        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mPreHolder == holder) {
                    if (onItemClickedListener != null) {
                        onItemClickedListener.onItemClicked(position);
                    }
                    return;
                }

                selectedPository = position;
                if (null == mPreHolder) {
                    holder.setChecked(R.id.rb_check, true);
                } else {
                    holder.setChecked(R.id.rb_check, true);
                    mPreHolder.setChecked(R.id.rb_check, false);
                }
                mPreHolder = holder;

                if (onItemClickedListener != null) {
                    onItemClickedListener.onItemClicked(position);
                }
            }
        });
    }

    private OnItemClickedListener onItemClickedListener;

    public void setOnItemClickedListener(OnItemClickedListener onItemClickedListener) {
        this.onItemClickedListener = onItemClickedListener;
    }

    public interface OnItemClickedListener {

        void onItemClicked(int position);
    }

}
